//
//  LJMainViewController.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/15.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit

class LJMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //首页
        let homeVC = LJHomeViewController()
        addChildVC(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC = LJMessageViewController()
        addChildVC(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //发现
        let discoverVC = LJDiscoverViewController()
        addChildVC(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVC = LJProfileViewController()
        addChildVC(profileVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    /**
    添加跟控制器的方法
    
    - parameter controller: 控制器
    - parameter title:      控制器顶部导航栏名称
    - parameter imageName:  控制器底部导航栏图片名
    */
    private func addChildVC (controller: UIViewController, title: String, imageName: String) {
        
        let tabBar = LJMainTabBar { () -> () in
            if LJUserAccount.isUserLogin {
                self.presentViewController(UINavigationController(rootViewController: LJComposeViewController()), animated: true, completion: nil)
            }else{
                // 创建授权控制器
                let OauthCV = LJOauthViewController()
                
                self.presentViewController(UINavigationController(rootViewController: OauthCV), animated: true, completion: nil)
            }
            
        }
        //使用kvc赋值
        self.setValue(tabBar, forKey: "tabBar")
        
        //设置控制器导航栏名称
        controller.title = title
        
        controller.tabBarItem.image = UIImage(named: imageName)
        let selectImageName = imageName + "_highlighted"
        
        //设置底部导航栏图片渲染
        controller.tabBarItem.selectedImage = UIImage(named: selectImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置导航条字体颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        let nav = UINavigationController(rootViewController: controller)
        
        addChildViewController(nav)
    }
}
